﻿using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using PremierTaxFree.PTFLib.Data;
using PremierTaxFree.PTFLib.Extensions;
using PremierTaxFree.PTFLib.Security;

namespace GdiTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.BackgroundImage = button1.SnapShot();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            IntPtr p = new IntPtr(int.Parse(textBox1.Text));
            const int MIN = 10, MAX = 100;
            new DrawDelegate((g) => { g.DrawLine(SystemPens.ActiveCaption, Point.Empty.Random(MIN, MAX), Point.Empty.Random(MIN, MAX)); }).DrawToWnd(p);
        }

        [DllImport("user32.dll")]
        static extern bool UpdateWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);

        private void button3_Click(object sender, EventArgs e)
        {
            IntPtr p2 = new IntPtr(int.Parse(textBox1.Text));
            InvalidateRect(p2, IntPtr.Zero, true);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DataAccess.ResetDatabase_Client();
            //DataAccess.CreateDb();
            //DataAccess.CreateTables();
            for (int i = 0; i < 100; i++)
            {
                DataAccess.InsertFileAsync_Client("Test" + i, "Testtesttest" + i);
            }

            SQLWorker.Default.Empty.WaitOne();
            var list = DataAccess.SelectFilesForExport_Client();
            DataAccess.DeleteOlderSentFiles_Client();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            ProtectedXml xml = new ProtectedXml(ProtectionOptions.SignAndEncript);
            var t = new Hashtable();
            t.Add("CountryCode", "12345");
            t.Add("Business", 20);
            t.Add("RetailerID", "RD1123332");
            t.Add("VoucherID", "1234456-11111");
            t.Add("VoucherImage", Image.FromFile(@"D:\ICONES\abstain-big.gif").ToArray());
            t.Add("BarCodeImage", Image.FromFile(@"D:\ICONES\5z5zeb4.gif").ToArray());
            xml.AddElement("voucher", t);
            xml.AddElement("voucher", t);
            xml.Save("d:\\test.xml");

            ProtectedXml xml2 = new ProtectedXml(ProtectionOptions.SignAndEncript);
            xml2.Load("d:\\test.xml");

            DataTable table = new DataTable();
            table.Columns.AddRange(new DataColumn[]
            {
                new DataColumn("CountryCode", typeof(string)),
                new DataColumn("Business", typeof(int)),
                new DataColumn("RetailerID", typeof(string)),
                new DataColumn("VoucherID", typeof(string)),
                new DataColumn("VoucherImage", typeof(byte[])),
                new DataColumn("BarCodeImage", typeof(byte[])),
            });
            xml2.WriteTo(table);
            Debug.Assert(table != null);
        }
    }
}